package controllers

import (
	"baokaobang/utils"
	"fmt"
	"strconv"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/sessions"
	rc "github.com/rongcloud/server-sdk-go/v3/sdk"
)

type GroupQuit struct {
	Ctx iris.Context
}

//删除群成员
func (c *GroupQuit) PostQuit() interface{} {
	session := sessions.Get(c.Ctx)	
	channel := c.Ctx.PostValue("channel")
	collegeCode := c.Ctx.PostValue("collegeCode")

	if collegeCode == "" || channel == "" {
		return utils.CommonRes(1000, fmt.Sprintf("获取参数有误"), "")
	}

	//获取userID
	userId, _ := session.GetInt64(fmt.Sprintf("%s userId", channel))
	userID := strconv.FormatInt(userId, 10)

	//创建融云对象
	//创建融云对象  传入appkey、appsecret
	rongCloud := rc.NewRongCloud("x18ywvqfx4c1c", "Y3Vo93Qk1cC")
	if rongCloud == nil {
		return utils.CommonRes(1000, fmt.Sprintf("创建融云对象失败"), "")
	}

	//退出群组
	err := rongCloud.GroupQuit(collegeCode, userID)
	if err != nil {
		return utils.CommonRes(1000, fmt.Sprintf("退出群组失败: %s", err.Error()), "")
	}

	return utils.CommonRes(20000, fmt.Sprintf("退出群组成功"), "")
}
